using UnityEngine;
using QFramework;
using System.Linq;

// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class SimpleKnifes : ViewController,IController,ISkill,ICanReadModels
	{
        public string SkillName => "抛射飞刀";

        public bool IsUnLocked { get; set; }

        public void CallBack()
        {
            
        }

        public IArchitecture GetArchitecture()
        {
			return Global.Interface;
        }

        public void ReadModels()
        {
            this.RegisterEvent<RemoveBulletEvent>(e =>
            {
                Debug.Log("获取到换弹");
                SetKnifes();

            }).UnRegisterWhenGameObjectDestroyed(gameObject);

        }

        public void RemoveCallBack()
        {
            
        }

        void SetKnifes()
        {
            var enemys = FindObjectsByType<EnemyFathers>
                (FindObjectsInactive.Exclude,
                FindObjectsSortMode.None).
                OrderBy(item => Player.Instance.
                Distance2D(item)).
                Take(DataManager.Instance.simpleKnifeModel.
                AbilityItemCount.Value
                +DataManager.Instance.abilityUpModel.
                AbilitysItemCount.Value); 

          
                
                foreach (var enemy in enemys)
                {
                    if(enemy != null)
                    {

                     
                        KnifeBullet.Instantiate().Position(this.gameObject.Position()).Show().Self(self =>
                        {
                            Debug.Log("生成飞刀");

                            var selfChage = self;
                            self.SetBulletScale(DataManager.Instance.simpleKnifeModel.AbilityItemScale.Value);
                            self.SetBulletSpeed(DataManager.Instance.simpleKnifeModel.AbilityItemSpeed.Value);
                            //var direction = enemy.NormalizedDirection2DFrom(Player.Instance);
                            self.WithEnemys(enemy);
                        });
                    }
                }
           

           
        }




        void Start()
		{
            // Code Here

            ReadModels();


        }

		
	}
}
